/****************************************************************************
** Uses generated components from analyzing all variables, and studies their*
** relationships with cognitive and demographic factors						*
**																			*
** Produced by Erik Snowberg, October 13, 2022								*
** 		Based on Code by Jonathan Chapman and Erik Snowberg					*
****************************************************************************/

/**********************************************************************
** Create the correlation matrix
**********************************************************************/
/**********************************
**Define macros needed to run code
***********************************/
// List of variables with one measure
global  oneByTwoListNotEV = `"IQSum CRT incomeLinear education age male churchAttendanceLinear_pdl "' ///
							+ `"avgEconComponent1 avgEconComponent2 avgEconComponent3 "' ///
							+ `"avgEconComponent4 avgEconComponent5 avgEconComponent6"'
	
/*****************
**Run the Code
******************/
// This defines a program to run the non-orthogonal ORIV procedure, 
// and then constructs separate matrices with the correlations and SEs
use "${datapath}econographicsTransformedWithComponents.dta", clear
do "${dofilepath}createCorrelationMatrixForPCA.do"

///***********************************************************************
// 5.1: Correlations with IQ
///***********************************************************************

#delimit;
local varlistIQ "IQSum CRT avgEconComponent1 avgEconComponent2
			avgEconComponent3 avgEconComponent4 avgEconComponent5 avgEconComponent6";
#delimit cr

// Need to create a subset of the correlation matrix with these variables
** First the columns
cap matrix drop B
local r=rowsof(corr2)
foreach i in `varlistIQ'  {
  matrix B= nullmat(B), corr2[1..`r',colnumb(corr2,"`i'")]
}
** Now the rows
cap matrix drop C
local c=colsof(B)
foreach i in  `varlistIQ'  {
  matrix C= nullmat(C) \ B[rownumb(B,"`i'"),1..`c']  
}

cap mat drop tmpMIQ
mat rename C tmpMIQ

// Start by creating a subset of the SE matrix to match the correlation matrix
** First the columns
cap matrix drop Bse
local r=rowsof(se)
display "`r'"
foreach i in `varlistIQ' {
  matrix Bse= nullmat(Bse), se[1..`r',colnumb(se,"`i'")]
}
** Now the rows
cap matrix drop Cse
local c=colsof(Bse)
foreach i in  `varlistIQ'  {
  matrix Cse= nullmat(Cse) \ Bse[rownumb(Bse,"`i'"),1..`c']  
}
cap mat drop tmpMIQSE
mat rename Cse tmpMIQSE

cap mat drop MIQ MIQSE
matrix MIQ = tmpMIQ[3..8, 1..2]
matrix MIQSE = tmpMIQSE[3..8, 1..2]

// Now reorganize the varib
cap mat drop O
local r=rowsof(MIQ)
foreach i in IQSum CRT {
	matrix O=nullmat(O),MIQ[1..`r',colnumb(MIQ,"`i'")]
	matrix O=nullmat(O),MIQSE[1..`r',colnumb(MIQSE,"`i'")]
}

#delimit;
frmttable using ${outputpath}IQ.tex,
		replace fragment plain nocenter substat(1) tex statmat(O) sdec(2\3) 
		ctitles("", "IQ", "CRT")
		rtitles("Generosity" \ "" \ 
				"Risk Aversion:" \ "~~WTA" \ 
				"WTP /" \ "~~Inequality" \ 
				"Overconfidence" \ "" \ 
				"Impulsivity /" \ "" \ 
				"Uncertainty" \ "" );
#delimit cr	


///***********************************************************************
// 5.2 Correlations with Demographics
///***********************************************************************
#delimit;
local varlistDemo "incomeLinear education age male churchAttendanceLinear_pdl 
			avgEconComponent1 avgEconComponent2 avgEconComponent3 
			avgEconComponent4 avgEconComponent5 avgEconComponent6";
#delimit cr

// Need to create a subset of the correlation matrix with these variables
** First the columns
cap matrix drop B
local r=rowsof(corr2)
foreach i in `varlistDemo'  {
  matrix B= nullmat(B), corr2[1..`r',colnumb(corr2,"`i'")]
}
** Now the rows
cap matrix drop C
local c=colsof(B)
foreach i in  `varlistDemo'  {
  matrix C= nullmat(C) \ B[rownumb(B,"`i'"),1..`c']  
}

cap mat drop tmpMDemo
mat rename C tmpMDemo

// Start by creating a subset of the SE matrix to match the correlation matrix
** First the columns
cap matrix drop Bse
local r=rowsof(se)
display "`r'"
foreach i in `varlistDemo' {
  matrix Bse= nullmat(Bse), se[1..`r',colnumb(se,"`i'")]
}
** Now the rows
cap matrix drop Cse
local c=colsof(Bse)
foreach i in  `varlistDemo'  {
  matrix Cse= nullmat(Cse) \ Bse[rownumb(Bse,"`i'"),1..`c']  
}
cap mat drop tmpMDemoSE 
mat rename Cse tmpMDemoSE

cap mat drop MDemo MDemoSE
matrix MDemo = tmpMDemo[6..11, 1..5]
matrix MDemoSE = tmpMDemoSE[6..11, 1..5]

// Now reorganize the varib
cap mat drop O
local r=rowsof(MDemo)
foreach i in incomeLinear education age male churchAttendanceLinear_pdl {
	matrix O=nullmat(O),MDemo[1..`r',colnumb(MDemo,"`i'")]
	matrix O=nullmat(O),MDemoSE[1..`r',colnumb(MDemoSE,"`i'")]
}


#delimit;
frmttable using ${outputpath}Demo.tex,
		replace fragment plain nocenter substat(1) tex statmat(O) sdec(2\3) 
		annotate(highlight) asymbol("\hltDneg" "\hltCneg" "\hltBneg" "\hltAneg" "" "\hltApos" "\hltBpos" "\hltCpos" "\hltDpos") 
		ctitles("", "Income", "Education", "Age", "Male", "Church")
		rtitles("Generosity" \ "" \ 
				"Risk Aversion:" \ "~~WTA" \ 
				"WTP /" \ "~~Inequality" \ 
				"Overconfidence" \ "" \ 
				"Impulsivity" \ "" \ 
				"Uncertainty" \ "" );
#delimit cr


///***********************************************************************
// 4.2 Panel B: controlling for IQ
///***********************************************************************
use "${datapath}econographicsTransformedWithComponents.dta", clear

// List of variables with one measure where equal variance should not apply
global  oneByTwoListNotEV = `"incomeLinear education age male churchAttendanceLinear_pdl "' ///
							+ `"avgEconComponent1 avgEconComponent2 avgEconComponent3 "' ///
							+ `"avgEconComponent4 avgEconComponent5 avgEconComponent6"'
local varlistDemoResid = ""
foreach var of varlist $oneByTwoListNotEV {
	reg `var' IQSum
	predict `var'Resid, resid
	local varlistDemoResid = "`varlistDemoResid' `var'Resid" 
}

global oneByTwoListNotEV = "`varlistDemoResid'"

// This defines a program to run the non-orthogonal ORIV procedure, 
// and then constructs separate matrices with the correlations and SEs
do "${dofilepath}createCorrelationMatrixForPCA.do"

cap mat drop MDemoResid MDemoResidSE
matrix MDemoResid = corr2[6..11, 1..5]
matrix MDemoResidSE = se[6..11, 1..5]

// Now reorganize the varib
cap mat drop O
local r=rowsof(MDemoResid)
foreach i in incomeLinearResid educationResid ageResid maleResid churchAttendanceLinear_pdlResid {
	matrix O=nullmat(O),MDemoResid[1..`r',colnumb(MDemoResid,"`i'")]
	matrix O=nullmat(O),MDemoResidSE[1..`r',colnumb(MDemoResidSE,"`i'")]
}

	
#delimit;
frmttable using ${outputpath}DemoResid.tex,
		replace fragment plain nocenter substat(1) tex statmat(O) sdec(2\3) 
		ctitles("", "Income", "Education", "Age", "Male", "Church")
		rtitles("Generosity" \ "" \ 
				"Risk Aversion:" \ "~~WTA" \ 
				"WTP /" \ "~~Inequality" \ 
				"Overconfidence" \ "" \ 
				"Impulsivity" \ "" \ 
				"Uncertainty" \ "" );
#delimit cr


/**********************************************************************
** Create the correlation matrix for appnedix tables
**********************************************************************/
/**********************************
**Define macros needed to run code
***********************************/
// List of variables with two measures. Note that it is assumed that these measures
// are labeled "1" and "2". Hence the renaming of some variables
global  twoByTwoList =`"WTP WTA riskAversionCRSure riskAversionCRLottery "' ///		
					+ `"advanInequalityAversion disadInequalityAversion patience riskAversionGain "' ///
					+ `"riskAversionMixGainLoss riskAversionLoss "' ///		
					+ `"ambiguityAversion compoundAversion  "' ///
					+ `"overPlacementFacts overEstimateIQ reciprocityHigh reciprocityLow "' ///
					+ `"qualOverPrecise "' 
// List of variables with measures where non-orthogonal error is an issue 
// since the measures are constructed with common compoenents
** These need to entered in PAIRS relating to the correlation that should be implemented
** with the separate procedure
** e.g. if want the correlation with endowment effect and WTP and also WTA, endowment effect enters twice
** NOTE: all these variables should also be included in the twoByTwoList above
/*global  twoByTwoListNonOrthog =`"ambiguityAversion compoundAversion "' ///
							+ `"qualOverPrecise overPlacementFacts "' /// 
							+ `"overEstimateIQ overPlacementIQ "' */

// List of variables with one measure where equal variance should apply		
global  oneByTwoListEV =`"altruism trust punishSender punishReceiver"'
					
// List of variables with one measure where equal variance should not apply
global  oneByTwoListNotEV = `"IQSum CRT incomeLinear education age male churchAttendanceLinear_pdl"'
	
/*****************
**Run the Code
******************/
// This defines a program to run the non-orthogonal ORIV procedure, 
// and then constructs separate matrices with the correlations and SEs
use "${datapath}econographicsTransformedWithComponents.dta", clear
do "${dofilepath}createCorrelationMatrixForPCA.do"


///***********************************************************************
// Appendix: Econographic Correlations with IQ / Demographics
///***********************************************************************
#delimit;
local varlistEconographics "IQSum CRT incomeLinear education age male churchAttendanceLinear_pdl 
			reciprocityLow reciprocityHigh altruism trust punishSender 
			punishReceiver advanInequalityAversion disadInequalityAversion 
			WTA riskAversionGain riskAversionLoss riskAversionMixGainLoss 
			WTP riskAversionCRSure riskAversionCRLottery 
			ambiguityAversion compoundAversion
			overEstimateIQ overPlacementFacts qualOverPrecise patience";
#delimit cr

// Need to create a subset of the correlation matrix with these variables
** First the columns
cap matrix drop B
local r=rowsof(corr2)
foreach i in `varlistEconographics'  {
  matrix B= nullmat(B), corr2[1..`r',colnumb(corr2,"`i'")]
}
** Now the rows
cap matrix drop C
local c=colsof(B)
foreach i in  `varlistEconographics'  {
  matrix C= nullmat(C) \ B[rownumb(B,"`i'"),1..`c']  
}

cap mat drop tmpMEconographics
mat rename C tmpMEconographics

// Start by creating a subset of the SE matrix to match the correlation matrix
** First the columns
cap matrix drop Bse
local r=rowsof(se)
display "`r'"
foreach i in `varlistEconographics' {
  matrix Bse= nullmat(Bse), se[1..`r',colnumb(se,"`i'")]
}
** Now the rows
cap matrix drop Cse
local c=colsof(Bse)
foreach i in  `varlistEconographics'  {
  matrix Cse= nullmat(Cse) \ Bse[rownumb(Bse,"`i'"),1..`c']  
}
cap mat drop tmpMEconographicsSE 
mat rename Cse tmpMEconographicsSE

cap mat drop MEconographics MEconographicsSE

//from here
matrix MEconographics = tmpMEconographics[8..28, 1..7]
matrix MEconographicsSE = tmpMEconographicsSE[8..28, 1..7]

// Now reorganize the varib
cap mat drop O
local r=rowsof(MEconographics)
foreach i in IQSum CRT incomeLinear education age male churchAttendanceLinear_pdl {
	matrix O=nullmat(O),MEconographics[1..`r',colnumb(MEconographics,"`i'")]
	matrix O=nullmat(O),MEconographicsSE[1..`r',colnumb(MEconographicsSE,"`i'")]
}

#delimit;
frmttable using ${outputpath}APPEconIQDemo.tex , 
		tex statmat(O) sdec(2\3) replace fragment plain nocenter substat(1) 
		ctitles("", "IQ", "CRT", "Income", "Education", "Age", "Male", "Church")
		rtitles("Reciprocity: Low" \ "" \ 
				"Reciprocity: High" \ "" \ 
				"Altruism" \ "" \ 
				"Trust" \  "" \ 
				"Anti-Social" \ "~~Punishment" \ 
				"Pro-Social" \ "~~Punishment" \ 
				"Dislike" \ "~~Having More" \ 
				"Dislike" \ "~~Having Less" \ 
				"WTA" \ "" \ 
				"Risk Aversion:" \ "~~Gains" \ 
				"Risk Aversion:" \ "~~Losses" \ 
				"Risk Aversion:" \ "~~Gain / Loss" \
				"WTP" \  "" \ 
				"Risk Aversion:" \ "CR Certain" \ 
				"Risk Aversion:" \ "CR Lottery" \ 
				"Ambiguity" \ "~~Aversion" \ 
				"Compound Lott." \ "~~Aversion" \ 
				"Overestimation" \ "" \ 
				"Overplacement" \ "" \ 
				"Overprecision" \ "" \ 
				"Patience");
#delimit cr	



///***********************************************************************
// For the Appendix: CDF of strategic sophistication
///***********************************************************************
use "${datapath}econographicsTransformedWithComponents.dta", clear

gen strategic10 = -strategicSophistication1
gen strategic5 = -strategicSophistication2
egen tag10 = tag(strategic10)
egen tag5 = tag(strategic5)
cumul strategic10, gen(c10) equal
cumul strategic5, gen(c5) equal

#delimit;
twoway 	
	(line c10 strategic10 if tag10, sort lwidth(medium) lcolor(orange) ) 
	(line c5 strategic5 if tag5, sort lpattern(dash) lcolor(purple) lwidth(medium)),
	xtitle("Choice in Beauty Contest", size(small))
	ylabel(0 "0" 0.2 "0.2" 0.4 "0.4" 0.6 "0.6" 0.8 "0.8" 1 "1", angle(0) labsize(small))
	xlabel(, labsize(small))
	ytitle("CDF")
	legend(label(1 "10 People in Group") label(2 "5 People in Group") size(medium) cols(1) ring(0) position(4)) 
	graphregion(color(gs16))
	xsize(2.3) ysize(2)
	name(strategicSophistication, replace)
	;
#delimit cr

gr export "${outputpath}APPStrategicSophistication.eps", fontface(Times) replace
window manage close graph


